package com.youlu.campus.service.category;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.CategoryInfo;
import com.youlu.campus.entity.VO.req.CategoryQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

@Slf4j
@Service
public class CategoryInfoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    public Page<CategoryInfo> list(CategoryQueryVO categoryQuery){
        String name = categoryQuery.getName();
        String status = categoryQuery.getStatus();
        Query query = new Query();
        if(StringUtils.isNotBlank(name)){
            Pattern pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("name").regex(pattern));
        }
        if(StringUtils.isNotBlank(status)){
            query.addCriteria(Criteria.where("status").is(status));
        }else {
            if(StringUtils.isNotBlank(categoryQuery.getPlatform())){
                query.addCriteria(Criteria.where("status").is(CategoryInfo.CATEGORY_ENABLE));
            }
        }
        PageRequest pageRequest = PageRequest.of(categoryQuery.getPageNo() - 1, categoryQuery.getPageSize());
        Long count = mongoTemplate.count(query, CategoryInfo.class);
        if(count ==0L){
            return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
//        query.with(new Sort(Sort.Direction.DESC, "priority"));
        Sort sort=Sort.by(Sort.Direction.DESC, "priority");
        query.with(sort);
        List<CategoryInfo> categoryInfoList = mongoTemplate.find(query.with(pageRequest), CategoryInfo.class);
        return PageableExecutionUtils.getPage(categoryInfoList, pageRequest, () -> count);
    }

    public void saveOrUpdate(CategoryInfo saveCategory){
        String name = saveCategory.getName();
        if(StringUtils.isBlank(name)){
            throw new BusinessException("类目名称不能为空");
        }
        CategoryInfo categoryInfo = null;
        if(StringUtils.isNotBlank(saveCategory.getId())){
             categoryInfo = this.mongoTemplate.findById(saveCategory.getId(), CategoryInfo.class);
        }
        Date now = new Date();
        if(Objects.isNull(categoryInfo)){
            Query query = new Query();
            query.addCriteria(Criteria.where("name").is(name));
            boolean exists = this.mongoTemplate.exists(query, CategoryInfo.class);
            if(exists){
                throw new BusinessException("已存在类目,不能重复添加");
            }
            saveCategory.setCreatedTime(now);
        }
        saveCategory.setUpdatedTime(now);
        this.mongoTemplate.save(saveCategory);
    }




}
